Skip to content

Commit 790a14b

Browse files
authored
Delete non-approved revisions on user deletion (#6567)
* Delete docs without revisions on user deletion
1 parent 64a23aa commit 790a14b

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

kitsune/wiki/handlers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ def on_user_deletion(self, user: User) -> None:
2020
document.contributors.add(*content_group.user_set.all())
2121
document.contributors.remove(user)
2222

23+
non_approved_revisions = Revision.objects.filter(creator=user, is_approved=False)
24+
# Delete documents with no approved revisions
25+
Document.objects.filter(
26+
current_revision__isnull=True, revisions__in=non_approved_revisions
27+
).delete()
28+
# delete remaining non-approved revisions
29+
non_approved_revisions.delete()
30+
2331
sumo_bot = Profile.get_sumo_bot()
2432
Revision.objects.filter(creator=user).update(creator=sumo_bot)
2533
# Anonymize any revision votes.

kitsune/wiki/tests/test_handlers.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from kitsune.users.models import Profile
55
from kitsune.users.tests import GroupFactory, UserFactory
66
from kitsune.wiki.handlers import DocumentListener
7-
from kitsune.wiki.tests import DocumentFactory, HelpfulVoteFactory
7+
from kitsune.wiki.models import Document, Revision
8+
from kitsune.wiki.tests import DocumentFactory, HelpfulVoteFactory, RevisionFactory
89

910

1011
class TestDocumentListener(TestCase):
@@ -85,3 +86,41 @@ def test_anonymize_votes(self):
8586
# The rest should remain untouched.
8687
self.assertTrue(v2.creator)
8788
self.assertFalse(v2.anonymous_id)
89+
90+
def test_non_approved_revision_handling(self):
91+
"""Test handling of non-approved revisions when a user is deleted."""
92+
doc_to_delete = DocumentFactory(current_revision=None)
93+
RevisionFactory(document=doc_to_delete, creator=self.user, is_approved=False)
94+
95+
doc_to_keep = DocumentFactory()
96+
other_user = UserFactory()
97+
approved_rev = RevisionFactory(document=doc_to_keep, creator=other_user, is_approved=True)
98+
doc_to_keep.current_revision = approved_rev
99+
doc_to_keep.save()
100+
non_approved_rev = RevisionFactory(
101+
document=doc_to_keep, creator=self.user, is_approved=False
102+
)
103+
104+
doc_to_reassign = DocumentFactory()
105+
approved_user_rev = RevisionFactory(
106+
document=doc_to_reassign, creator=self.user, is_approved=True
107+
)
108+
doc_to_reassign.current_revision = approved_user_rev
109+
doc_to_reassign.save()
110+
111+
self.listener.on_user_deletion(self.user)
112+
113+
self.assertFalse(Document.objects.filter(id=doc_to_delete.id).exists())
114+
115+
self.assertTrue(Document.objects.filter(id=doc_to_keep.id).exists())
116+
doc_to_keep.refresh_from_db()
117+
118+
self.assertTrue(Revision.objects.filter(id=approved_rev.id).exists())
119+
120+
self.assertFalse(Revision.objects.filter(id=non_approved_rev.id).exists())
121+
122+
self.assertTrue(Document.objects.filter(id=doc_to_reassign.id).exists())
123+
doc_to_reassign.refresh_from_db()
124+
self.assertEqual(
125+
doc_to_reassign.current_revision.creator.username, settings.SUMO_BOT_USERNAME
126+
)

0 commit comments

Comments
 (0)